package com.example.fragment;


import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.example.adapter.ListViewAdapter;
import com.example.adapter.ListViewAdapter1;
import com.example.beans.PaperInformation;
import com.example.beans.WorkInfoemation;
import com.example.inter.UserInfoCallBack;
import com.example.myapplication.R;
import com.example.util.UserInfoWork;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentWorkInfo extends Fragment implements UserInfoCallBack {
    private ListView listView;
    private List<WorkInfoemation> list = new ArrayList<>();
    private ListViewAdapter1 listViewAdapter;
    private WorkInfoemation workInfoemation;
    private UserInfoWork userInfoTeacher;
    private TextView textView;
    public FragmentWorkInfo() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_paper_info,container,false);
        listView = (ListView) view.findViewById(R.id.listView);
        listViewAdapter = new ListViewAdapter1(getContext(),list);
        init(view);
        textView.setText("任务列表");
        listView.setAdapter(listViewAdapter);
        Bundle bundle = getArguments();
        workInfoemation = (WorkInfoemation) bundle.getSerializable("teacher");
        userInfoTeacher = new UserInfoWork();
        if (workInfoemation != null) {
            String userid =workInfoemation.getTeacher_id();
            userInfoTeacher.getInfo(userid,this);
        }else{
            Snackbar.make(view,"wrong",Snackbar.LENGTH_SHORT).show();
        }
        return view;
    }

    public void init(View view){
        textView = (TextView) view.findViewById(R.id.textname);
    }

    @Override
    public void onSuccess(String info) {
        try {
            JSONObject jsonObject = new JSONObject(info);
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                WorkInfoemation workInfoemation = new WorkInfoemation();
                String workid = jsonObject1.getString("workid");
                String workname = jsonObject1.getString("workname");
                String date = jsonObject1.getString("date");
                String teacher_id = jsonObject1.getString("teacher_id");
                workInfoemation.setWorkid(workid);
                workInfoemation.setWorkname(workname);
                workInfoemation.setDate(date);
                workInfoemation.setTeacher_id(teacher_id);
                list.add(workInfoemation);
            }
            listViewAdapter.changeList(list);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFail() {

    }
}
